#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include "ConsumerParser.h"

ConsumerParser::ConsumerParser(LoaderXML &stream) : _stream(stream), _bloc(""), index(0) {}

void    ConsumerParser::getBloc()
{
	this->_bloc += this->_stream.nextString();
}

bool            ConsumerParser::readText(char *txt)
{
	unsigned int          i = 0;
	SAVE_CONTEXT();
	while (this->readChar(txt[i]))
		i++;
	if (i == strlen(txt))
		return true;
	RESTORE_CONTEXT();
	return false;
}

bool    ConsumerParser::readEOF()
{
	NEW_BLOC();
	return (this->index == this->_bloc.size() - 1);//(this->_bloc[this->index] == EOF);
}

bool    ConsumerParser::readUntil(char c)
{
	SAVE_CONTEXT();
	while (this->_bloc[this->index] != c  && this->_bloc[this->index])
	{
		this->index++;
		NEW_BLOC();
	}
	if (this->_bloc[this->index++] == c)
		return true;
	RESTORE_CONTEXT();
	return false;
}

bool    ConsumerParser::readUntilEOF()
{
	NEW_BLOC();
	if (this->readEOF())
		return true;
	while (this->_bloc[this->index])
	{
		this->index++;
		if (this->readEOF())
			return true;
		NEW_BLOC();
	}
	return false;
}

bool    ConsumerParser::readInteger()
{
	bool  res = false;
	while (this->readRange('0', '9'))
	{
		res = true;
		NEW_BLOC();
	}
	return res;
}

bool    ConsumerParser::readIdentifier()
{
	bool  res = false;

	while (this->readRange('0', '9')
		|| this->readRange('a', 'z')
		|| this->readRange('A', 'Z')
		|| this->readChar('_'))
	{
		res = true;
		NEW_BLOC();
	}
	return res;
}

std::string	ConsumerParser::getTagContent()
{
	NEW_BLOC();
	std::string	content;
	int		begin = this->index;

	content = &(this->_bloc.c_str()[begin]);

	content = content.substr(0, content.find("<"));

	std::cout << "[DEBUG]" << content << "[/DEBUG]" << std::endl;

	return content;
}
